/* Creating other vote-share outcomes data*/

use "Analysis Data/voteshare_data.dta", clear

*UPA
preserve
keep if cand_party == "UPA"
rename voteshare_2014 voteshare_upa_2014
rename voteshare_2009 voteshare_upa_2009
keep state_name ac_num voteshare_upa_*
tempfile upa
save `upa', replace
restore


*State CM Party
preserve
gen state_cm = 0
replace state_cm = 1 if cand_party == "UPA" & state_name == "Andhra Pradesh"
replace state_cm = 1 if cand_party == "NDA" &  state_name == "Bihar"
replace state_cm = 1 if cand_party == "NDA" &  state_name == "Chattisgarh"
replace state_cm = 1 if cand_party == "NDA" & state_name == "Jharkhand"
replace state_cm = 1 if cand_party == "UPA" & state_name == "Maharashtra"
replace state_cm = 1 if cand_party == "BJD" & state_name == "Orissa"
replace state_cm = 1 if cand_party == "UPA" & state_name == "Karnataka"
replace state_cm = 1 if cand_party == "SP" & state_name == "Uttar Pradesh"
replace state_cm = 1 if cand_party == "NDA" & state_name == "Madhya Pradesh"
replace state_cm = 1 if cand_party == "NDA" & state_name == "Rajasthan"

keep if state_cm==1

rename voteshare_2014 voteshare_statecm_2014
rename voteshare_2009 voteshare_statecm_2009

bys state_name ac_num: drop if _n >1

keep state_name ac_num voteshare_statecm_*
tempfile statecm
save `statecm', replace 

*Incumbent MP Party
 **Finding incumbent
use "Raw Data/results_10states2009_clean.dta", clear
bys state_name pc_num cand_num: egen total_cand_votes = sum(cand_votes)
bys state_name pc_num: egen max = max(total_cand_votes)
keep if total_cand_votes==max
drop max total_cand_votes
drop if ac_num<0
gen voteshare_inc_2009 = 100*cand_votes/total_ac_votes
keep state_name ac_num pc_num cand_party voteshare_inc_2009

*Coding alliances
replace cand_party = "NDA" if cand_party == "BJP"
replace cand_party = "NDA" if cand_party == "TDP" & state_name=="Andhra Pradesh"
replace cand_party = "NDA" if cand_party == "SHS" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "RSPS" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "RPI(A)" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "SWP" & state_name=="Maharashtra"
replace cand_party = "NDA" if cand_party == "LJP" & state_name=="Bihar"
replace cand_party = "NDA" if cand_party == "BLSP" & state_name=="Bihar"
replace cand_party = "NDA" if cand_party == "AD" & state_name=="Uttar Pradesh"

replace cand_party = "UPA" if inlist(cand_party,"INC","RJD","BVA","MD")
replace cand_party = "UPA" if cand_party == "JMM" & state_name == "Jharkhand"
replace cand_party = "UPA" if cand_party == "NCP" & state_name == "Maharashtra"
replace cand_party = "UPA" if cand_party == "RLD" & state_name == "Uttar Pradesh"

tempfile inc_party
save `inc_party', replace

restore
preserve
merge 1:1 state_name ac_num cand_party using `inc_party', keep(matched) nogen
gen voteshare_inc_2014 = 100*cand_votes_2014/total_ac_votes_2014
keep state_name ac_num voteshare_inc_*
tempfile incumbent
save `incumbent', replace
restore 

*AAP
preserve
keep if cand_party == "AAAP"
rename voteshare_2014 voteshare_aap_2014
keep state_name ac_num voteshare_aap_2014
tempfile aap
save `aap', replace
restore

*NOTA
preserve
gen voteshare_nota_2014 = 100*nota_votes_2014/total_ac_votes_2014
bys state_name ac_num: drop if _n>1
keep state_name ac_num voteshare_nota_2014
tempfile nota
save `nota', replace
restore

*Accused of Crimes & Richest
preserve
use "Analysis Data/voteshare_data.dta", clear

merge m:1 state_name pc_num cand_party using "Raw Data/affidavit_data.dta", keep(matched) nogen

gen criminal = (num_cases>0)

gen assets_mil = round(assets/10^6,1)

bys state_name pc_num: egen max_assets = max(assets_mil)
bys state_name pc_num: gen richest = (assets_mil == max_assets)

bys state_name ac_num: egen voteshare_crime_2014 = total(voteshare_2014/(criminal==1))
bys state_name ac_num: egen voteshare_crime_2009 = total(voteshare_2009/(criminal==1))

bys state_name ac_num: egen voteshare_rich_2014 = total(voteshare_2014/(richest==1))
bys state_name ac_num: egen voteshare_rich_2009 = total(voteshare_2009/(richest==1))

keep state_name ac_num voteshare_*_* 

bys state_name ac_num: drop if _n>1

replace voteshare_crime_2014 = . if voteshare_crime_2014 ==0

tempfile crime_rich
save `crime_rich', replace
restore


*Events
preserve
rename cand_party party_name
merge m:1 state_name pc_num party_name using "Analysis Data/events.dta", keep(matched) nogen

bys state_name ac_num: egen voteshare_events_2014 = sum(voteshare_2014)
bys state_name ac_num: egen voteshare_events_2009 = sum(voteshare_2009)

bys state_name ac_num: drop if _n >1

drop cand_votes* voteshare_???? party_name

order state_name ac_num pc_num voteshare_events_2014 voteshare_events_2009
tempfile events
save `events', replace
restore

*Volunteers
preserve
rename cand_party party_name

merge m:1 state_name pc_num party_name using "Analysis Data/volunteers.dta", keep(matched) nogen

bys state_name ac_num: egen voteshare_volunteers_2014 = sum(voteshare_2014)
bys state_name ac_num: egen voteshare_volunteers_2009 = sum(voteshare_2009)

bys state_name ac_num: drop if _n >1

drop cand_votes* voteshare_???? party_name

order state_name ac_num pc_num voteshare_volunteers_2014 voteshare_volunteers_2009
tempfile volunteers
save `volunteers', replace
restore


*Leaders
preserve
rename cand_party party_name

merge m:1 state_name pc_num party_name using "Analysis Data/leaders.dta", keep(matched) nogen

bys state_name ac_num: egen voteshare_leaders_2014 = sum(voteshare_2014)
bys state_name ac_num: egen voteshare_leaders_2009 = sum(voteshare_2009)

bys state_name ac_num: drop if _n >1

drop cand_votes* voteshare_???? party_name

order state_name ac_num pc_num voteshare_leaders_2014 voteshare_leaders_2009
tempfile leaders
save `leaders', replace
restore


*Spend
preserve
rename cand_party party_name

merge m:1 state_name pc_num party_name using "Analysis Data/spend.dta", keep(matched) nogen

bys state_name ac_num: egen voteshare_spend_2014 = sum(voteshare_2014)
bys state_name ac_num: egen voteshare_spend_2009 = sum(voteshare_2009)

bys state_name ac_num: drop if _n >1

drop cand_votes* voteshare_???? party_name

order state_name ac_num pc_num voteshare_spend_2014 voteshare_spend_2009
tempfile spend
save `spend', replace
restore

*Win
preserve
rename cand_party party_name

merge m:1 state_name pc_num party_name using "Analysis Data/win.dta", keep(matched) nogen

bys state_name ac_num: egen voteshare_win_2014 = sum(voteshare_2014)
bys state_name ac_num: egen voteshare_win_2009 = sum(voteshare_2009)

bys state_name ac_num: drop if _n >1

drop cand_votes* voteshare_???? party_name

order state_name ac_num pc_num voteshare_win_2014 voteshare_win_2009
tempfile win
save `win', replace
restore

*Merging
use "Analysis Data/AC_est_sample.dta", clear

merge 1:1 state_name ac_num using `upa', keep(master matched) nogen
merge 1:1 state_name ac_num using `statecm', keep(master matched) nogen
merge 1:1 state_name ac_num using `incumbent', keep(master matched) nogen
merge 1:1 state_name ac_num using `upa', keep(master matched) nogen
merge 1:1 state_name ac_num using `aap', keep(master matched) nogen
merge 1:1 state_name ac_num using `nota', keep(master matched) nogen
merge 1:1 state_name ac_num using `crime_rich', keep(master matched) nogen
merge 1:1 state_name ac_num using `events', keep(master matched) nogen
merge 1:1 state_name ac_num using `volunteers', keep(master matched) nogen
merge 1:1 state_name ac_num using `leaders', keep(master matched) nogen
merge 1:1 state_name ac_num using `spend', keep(master matched) nogen
merge 1:1 state_name ac_num using `win', keep(master matched) nogen

tab poll_date, gen(poll_date)

save "Analysis Data/table2_data.dta", replace
outsheet using "Table 2/table2_data.csv", comma nolabel replace

